package main

import (
	"flag"
	"fmt"
	"io"
	"time"
)

type RMB float64

func (r RMB) String() string {
	f := float64(r)
	return fmt.Sprintf("%d元%d角%d分", int(f/1), int(f*10)%10, int(f*100)%10)
}

func (r *RMB) Set(s string) error {
	var f float64
	var unit rune
	_, e := fmt.Sscanf(s, "%f%c", &f, &unit)
	if e != io.EOF && e != nil {
		return fmt.Errorf("fmt.Sscanf(%s)\n", e)
	}

	switch unit {
	case '元', '块', '￥':
		*r = RMB(f)
		return nil
	case 0:
		*r = RMB(f)
		return nil
	}

	return fmt.Errorf("wrong format\n")
}

func main() {
	var dur = flag.Duration("sleep", time.Second*1, "sleep period")
	var q RMB

	flag.CommandLine.Var(&q, "rmb", "china rmb")
	flag.Parse()
	fmt.Printf("sleep for %s\n", *dur)
	time.Sleep(*dur)
	fmt.Printf("%s\n", q)
}
